package com.huaxin.hxcloud.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseFragment;
import com.huaxin.common.Entity.BaseDTO;
import com.huaxin.common.Entity.UserDTO;
import com.huaxin.common.Global;
import com.huaxin.common.utils.DialogUtil;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/3/3.
 */

public class ModifySexFragment extends BaseFragment {

    @BindView(R.id.more)
    TextView     more;
    @BindView(R.id.toolbar)
    Toolbar      toolbar;
    @BindView(R.id.ig_check_male)
    ImageView    igCheckMale;
    @BindView(R.id.ll_male)
    LinearLayout llMale;
    @BindView(R.id.ig_check_female)
    ImageView    igCheckFemale;
    @BindView(R.id.ll_female)
    LinearLayout llFemale;

    String  _sex;
    UserDTO _user;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_modify_sex;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        more.setVisibility(View.VISIBLE);
        more.setText("确认");
    }

    @Override
    protected void onResumeView() {
        super.onResumeView();
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        toolbar.setTitle("修改性别");
        _user = Global.user;
        toggle(_user.getSex());
    }

    @OnClick({R.id.ll_male, R.id.ll_female, R.id.more})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_male:
                toggle("0");
                break;
            case R.id.ll_female:
                toggle("1");
                break;
            case R.id.more:
                if (_sex != _user.getSex()) {
                    ApiModifySex(_sex);
                } else {
                    finish();
                }
            default:
                break;
        }
    }

    private void ApiModifySex(final String sex) {
        final Dialog dialog = DialogUtil.getWaitDialog(getContext(), "提交中");
        dialog.show();
        Call<BaseDTO> call = getRequestApi().modifySex(sex);
        call.enqueue(new Callback<BaseDTO>() {
            @Override
            public void onResponse(Call<BaseDTO> call, Response<BaseDTO> response) {
                dialog.dismiss();
                if (response.isSuccessful()) {
                    BaseDTO baseDTO = response.body();
                    if (baseDTO != null && baseDTO.isSuccessful()) {
                        ToastUtil.showToast("修改成功");
                        _user.setSex(sex);
                        finish();
                    } else {
                        ToastUtil.showToast(baseDTO.getMsg());
                    }
                } else {
                    ToastUtil.showToast("数据异常");
                }
            }

            @Override
            public void onFailure(Call<BaseDTO> call, Throwable t) {
                dialog.dismiss();
                ToastUtil.showToast("网络故障");
            }
        });

    }

    private void toggle(String sex) {
        if (TextUtils.isEmpty(sex)) {
            igCheckMale.setVisibility(View.INVISIBLE);
            igCheckFemale.setVisibility(View.INVISIBLE);
            return;
        }
        _sex = sex;
        if (Integer.parseInt(sex) == 0) {
            igCheckMale.setVisibility(View.VISIBLE);
            igCheckFemale.setVisibility(View.INVISIBLE);
        } else {
            igCheckMale.setVisibility(View.INVISIBLE);
            igCheckFemale.setVisibility(View.VISIBLE);
        }

    }


}
